/*******************************************************************************

This code file produces Figures 3 ("Distribution of Buildings' 421-a Participation Thresholds"), A10 ("It Costs More to Put Inclusionary Units in Higher-Rent Neighborhoods"), and A13 ("Distribution of Buildings' 421-a Participation Thresholds, as Fractions of Market Value").

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
		
********************************************************************************
* Distribution of developer breakevens
********************************************************************************

*** Construct breakevens	
	
	* Run logit, store parameters
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta)
	mat b = e(b)
	local coef_dtaxrate = b[1,1]
	predict xb, xb
	predict pr, pr
	
	* Construct developer breakeven
	gen taxbill = assesstot*dtaxrate_onsite/(frac_assess*underassess)
	gen thresh = (1/0.2)*((assesstot-assessland)/(frac_assess*underassess*unitsres*1000000))*(dtaxrate_onsite - xb/`coef_dtaxrate')
	gen thresh_pp = dtaxrate_onsite - xb/`coef_dtaxrate'
	
*** Analyze means and binscatter correlations w/ the breakevens

	summ thresh, d

	* Plot distribution of breakevens
	
	replace thresh = 5 if thresh > 5 & !missing(thresh)
	replace thresh = 0 if thresh < 0 & !missing(thresh)
		
	twoway (hist thresh if thresh<=5, bin(50) freq ///
		graphregion(color(white)) xlabel(0(0.5)5) color(gs11)) ///
		(hist thresh if thresh<=5 & inclusionary_onsite == 1, bin(50) freq ///
		graphregion(color(white)) xlabel(0(0.5)5) color(gs5)), ///
		xtitle("Breakeven Cost Per Inclusionary Unit (Millions of Dollars)") ytitle("Number of Buildings", margin(small)) ///
		legend(order(2 "Has Inclusionary Units" 1 "No Inclusionary Units")) ylab(0(250)1250, nogrid)
			
	graph export "$figs/hist_breakevens_by_participation.pdf", replace
	graph export "$figs_overleaf/hist_breakevens_by_participation.pdf", replace
		
	* Binscatters
	
	gen meangrossrent = (12/(1000000*0.05))*exp(lmeangrossrent)
	
	binscatter thresh meangrossrent, ///
		xtitle("Capitalized Mean Gross Rent of Block Group") ytitle("Mean Breakeven Cost Per Inclusionary Unit") ///
		graphregion(color(white)) line(none) n(50) ylab(,nogrid)
		
	graph export "$figs/breakevens_by_rent_bg.pdf", replace
	graph export "$figs_overleaf/breakevens_by_rent_bg.pdf", replace
	
	* Breakeven distribution in terms of p.p. tax rate

	twoway (hist thresh_pp, bin(50) freq ///
		graphregion(color(white)) color(ltblue)) ///
		(hist thresh_pp if inclusionary_onsite == 1, bin(50) freq ///
		graphregion(color(white)) color(edkblue)), ///
		xtitle("Breakeven Cost of Participation (in Fraction of Market Value)") ytitle("Number of Buildings", margin(small)) ///
		legend(order(2 "Has Inclusionary Units" 1 "No Inclusionary Units")) ylab(0(250)1000, nogrid)
		
	graph export "$figs/hist_breakevens_pp_by_participation.pdf", replace
	graph export "$figs_overleaf/hist_breakevens_pp_by_participation.pdf", replace
